import random 
c = int(input('Введите четырёхзначное число: ')) 
max = 0 
if c > 9999 or c < 1000: 
    print ('0') 
def prost(n): 
    if n <= 1: 
        return False 
    for _ in range(5): 
        a = random.randint(1, n-1) 
        if pow(a, n-1, n) != 1: 
            return False 
        return True  
 
 
tfc = c // 1000 
if prost(tfc) == True: 
    max = tfc#1 
 
 
p = c // 100 
if prost(p) == True and p > max: 
    max =p#2 
 
 
bnk = c // 10 
if prost(bnk) == True and bnk > max: 
    max =bnk#3 
 
 
a = c 
if prost(a) == True and a > max: 
    max =a#4 
 
 
x = c // 100 % 10 
if prost(x) == True and x > max: 
    max = x#5 
 
 
s = c // 10 % 100 
if prost(s ) == True and s > max: 
    max =s#6 
 
 
m = c % 1000 
if prost(m) == True and m > max: 
    max = m#7 
 
 
d = c // 10 %10 
if prost(d) == True and d > max: 
    max = d#8 
 
 
fg = c % 100 
if prost(fg) == True and fg > max: 
    max = fg#9 
 
 
k = c % 10 
if prost(k) == True and k > max: 
    max = k#10 
 
 
print(max)